package com.swanclouds.redfish.chassis;

import com.google.common.base.Preconditions;
import com.swanclouds.api.chassis.ChassisService;
import com.swanclouds.api.chassis.PCIeDeviceService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.chassis.Chassis;
import com.swanclouds.model.chassis.PCIeDevices;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.chassis.domain.PCIeDevicesModel;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseChassisService;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: HuangFuYuHui
 * Title: PCIeDevicesServiceImpl
 * Description: TODO
 * Date: 2022-07-15 11:31.
 */
public class PCIeDevicesServiceImpl extends BaseChassisService implements PCIeDeviceService {

    private ChassisService chassisService = new ChassisServiceImpl();


    /**
     * 查询PCIe设备集合资源信息.
     * return: CollectionResource
     **/
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.PATH_CHASSIS_PCIEDEVICES,getChassisId())).execute();
    }

    /**
     * 查询指定服务器PCIe设备资源信息
     * @param pCIeDevicesId PCIe设备ID
     * @return com.swanclouds.model.chassis.PCIeDevices
     **/
    @Override
    public PCIeDevices getPCIeDevices(String pCIeDevicesId) {
        Preconditions.checkNotNull(pCIeDevicesId, "pCIeDevicesId is null");
        return get(PCIeDevicesModel.class, uri(ClientConstants.PATH_CHASSIS_PCIEDEVICES_ID,getChassisId(),pCIeDevicesId)).execute();
    }

    /**
     * 查询服务器PCIe设备集合资源信息。
     * @return java.util.List
     **/
    @Override
    public List<PCIeDevicesModel> getPCIeDevicesList() {
        List<PCIeDevicesModel> pcIeDevicesModels = new ArrayList<PCIeDevicesModel>();
        Chassis chassis = chassisService.get();
        chassis.getLinks().getpCIeDevices().stream().forEach(ref ->{
            PCIeDevicesModel execute = get(PCIeDevicesModel.class, uri(ref.getDataId())).execute();
            pcIeDevicesModels.add(execute);
        });
        return pcIeDevicesModels;
    }

}
